package com.youhong.teethcare.adapter

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.support.v4.graphics.drawable.DrawableCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.youhong.teethcare.R
import com.youhong.teethcare.javabeans.BrushingRecord
import com.youhong.teethcare.utils.Util

class BrushRecordAdapter(var recordList: List<BrushingRecord>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    class BrushRecordViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivScore = itemView.findViewById<ImageView>(R.id.iv_record_score)
        var ivNotConnected = itemView.findViewById<ImageView>(R.id.iv_recordNotConnected)
        var tvScore = itemView.findViewById<TextView>(R.id.tv_recordScore)
        var tvModeInfo = itemView.findViewById<TextView>(R.id.tv_modeInfo)
        var tvModeTime = itemView.findViewById<TextView>(R.id.tv_modeTime)
        var tvModeDuration = itemView.findViewById<TextView>(R.id.tv_modeDuration)
    }

    class EmptyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var tvEmpty = itemView.findViewById<TextView>(R.id.tvSpaceBottom)

    }
    fun updateData( recordList: List<BrushingRecord>){
        this.recordList=recordList;
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            itemViewType_Empty -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_empty_records, parent, false);
                EmptyViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(parent.context).inflate(R.layout.item_brushrecord_history, parent, false);
                BrushRecordViewHolder(view)
            }
        }

    }

    override fun onBindViewHolder(viewholder: RecyclerView.ViewHolder, position: Int) {

        when (getItemViewType(position)) {
            itemViewType_Empty -> {
                bindEmpty(viewholder as EmptyViewHolder)
            }
            else -> {
                bindContent(viewholder as BrushRecordViewHolder, position);
            }
        }
    }

    fun bindContent(viewholder: BrushRecordViewHolder, position: Int) {
        val context=viewholder.itemView.context;
        val brushingRecord = recordList[position];

        var type=brushingRecord.type;
        val date = brushingRecord.startTime;
        val duration = brushingRecord.totalTime;
        val str_totalTime = Util.concateZero(duration / 60) + ":" + Util.concateZero(duration % 60)
        val score = brushingRecord.percent;
        val tintColor=getTintColor(type,score)
        viewholder.tvModeDuration.text = str_totalTime;
        viewholder.tvModeTime.text=Util.analyzeTimeString(date)
        var drawableTint=tintDrawable(tintColor,viewholder.ivScore.drawable)
        viewholder.ivScore.setImageDrawable(drawableTint)
        when(type){
            0->{
                viewholder.ivNotConnected.visibility=View.GONE
                viewholder.tvScore.visibility=View.VISIBLE
                viewholder.tvScore.text = score.toString();
                viewholder.tvModeInfo.text=getShowModeInfo(brushingRecord,context)
            }
            else->{
                viewholder.ivNotConnected.visibility=View.VISIBLE
                viewholder.tvScore.visibility=View.GONE
                viewholder.tvModeInfo.text=context.getString(R.string.not_paired);
            }
        }

        viewholder.itemView.setOnClickListener {
                clickListener?.onItemClick(brushingRecord)
        }
    }

    fun getShowModeInfo(record:BrushingRecord,context:Context):String{
        var mode=record.brushMode;
        var score=record.percent;
        var modeName=context.resources.getStringArray(R.array.brushModes)[mode];
        var scoreLevel=Util.getBrushScoreLevel(score);
        var scoreText=context.resources.getStringArray(R.array.brushScoreLevels)[scoreLevel]
        var level=record.brushLevel;
        var showInfo="$modeName ${level+1} lv $scoreText";
        return showInfo;
    }
    fun tintDrawable(tintColor: Int, drawableRes: Drawable): Drawable {

        var drawableTarget = DrawableCompat.wrap(drawableRes.mutate());
        DrawableCompat.setTintList(drawableTarget, ColorStateList.valueOf(tintColor));
       return drawableTarget;
    }
    fun getTintColor(dataType:Int,score:Int):Int{
        var tintColor = 0;
        if(dataType==1){
            tintColor=Color.parseColor("#FF4300");
        }else{
            if (score > 60) {
                tintColor = Color.parseColor("#6091FF");
            } else {
                tintColor = Color.parseColor("#FF8500");
            }
        }
        return tintColor;
    }

    fun bindEmpty(viewholder: EmptyViewHolder) {

    }

    override fun getItemCount(): Int {
        return when (recordList.isEmpty()) {
            true -> 1
            else -> recordList.size;
        };

    }

    var itemViewType_Empty = 0;
    var itemViewType_Content = 1;
    override fun getItemViewType(position: Int): Int {
        return when (recordList.isEmpty()) {
            true -> itemViewType_Empty
            else -> itemViewType_Content;
        };
    }
    var clickListener: OnItemClickListener? = null;
    fun setItemClickListener(clickListener: OnItemClickListener) {
        this.clickListener = clickListener;
    }
    public interface OnItemClickListener {
        fun onItemClick(record: BrushingRecord) {
        };
    }
}